package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.text.AbstractDocument.Content;

import model.Field;
import model.Location;
import model.MapObject;
import model.Mob;
import model.SuperMob;


public class SimulatorView extends JFrame{

	private final String headLine = "LEVEL : " + " | " + " HP : ";
	private final String underLine = "Underskrift";
	private PanelFieldRect panelFieldRect;
	private JLabel headLiner, underLiner;
	private PanelButtom pb;
	private Field field;





	public SimulatorView(int depth, int width, Field field){
		
		this.field = field;
		setResizable(false);
		setTitle("Tower Defence");
		setLocation(50, 100);
		
		

		headLiner = new JLabel(headLine,JLabel.CENTER);
		underLiner = new JLabel(underLine,JLabel.CENTER);
		panelFieldRect = new PanelFieldRect(depth, width,field);


		pb = new PanelButtom(depth, width);


		Container contents = getContentPane();
		contents.add(headLiner,BorderLayout.NORTH);
		contents.add(underLiner,BorderLayout.SOUTH);
		contents.add(panelFieldRect,BorderLayout.CENTER);
		contents.add(pb,BorderLayout.EAST);


		pack();
		setVisible(true);


	}

	public void prePaintPanel(Field field){
		panelFieldRect.prePaint(field);
		
							
	}

	private class PanelFieldRect extends JPanel {

		private final int scalarFactor = 50;
		private int gridDepth;
		private int gridWidth;
		Dimension size;
		private Graphics g;
		private Image image;
		private Field field;



		public PanelFieldRect(int depth, int width, Field field){
			this.gridDepth = depth;
			this.gridWidth = width;
			size = new Dimension(0,0);
			this.field = field;
		
			
		}

		public Dimension getPreferredSize(){
			return new Dimension(gridWidth * scalarFactor, gridDepth * scalarFactor);

		}


		public void prePaint(Field field){
			this.field = field;
			this.gridDepth = field.getDepth();
			this.gridWidth = field.getWidth();
			
		}

		public void paint(Graphics g){

			for(int row = 0; row < field.getDepth() ;row++){
				for(int col = 0; col < field.getWidth(); col++){
					MapObject mo = (MapObject) field.getObjectAt(row, col);
					if(mo == null){
						g.setColor(Color.white);
						g.fillRect(col*scalarFactor,row*scalarFactor,scalarFactor-5,scalarFactor-5);
					
					}
					if(mo != null){
						g.setColor(mo.getColor());
						g.fillRect(col*scalarFactor,row*scalarFactor,scalarFactor-5,scalarFactor-5);
					}
				}
			}
						
		}



//		public void paintMark(MapObject mo){
//
//			g.setColor(color);
//			g.fillRect(col*scalarFactor,row*scalarFactor,scalarFactor-5,scalarFactor-5);
//
//		}


	}


	private class PanelButtom extends JPanel {

		private int gridHeight;
		private int gridWidth;
		private Dimension size;
		private JRadioButton jrbSniper, jrbRocket;
		private JButton jbBuy,jbCancel,jbMovement;


		public PanelButtom(int depth, int width){
			this.gridHeight = depth;
			this.gridWidth = width;
			setBackground(Color.GRAY);
			this.size = new Dimension(0,0);
			
			
			jrbSniper = new JRadioButton("Sniper Tower");
			//			jbSniper.setMnemonic(KeyEvent.VK_S);
			jrbSniper.addActionListener(new JRBSniperActionListener());
			jrbSniper.setBackground(Color.BLUE);
			this.add(jrbSniper);
			jrbSniper.setEnabled(true);


			jrbRocket = new JRadioButton("Rocket Tower");
			//			jbRocket.setMnemonic(KeyEvent.VK_R);
			jrbRocket.addActionListener(new JRBRocketActionListener());
			jrbRocket.setBackground(Color.BLUE);
			this.add(jrbRocket);
			jrbRocket.setEnabled(true);

			ButtonGroup bg = new ButtonGroup();
			bg.add(jrbSniper);
			bg.add(jrbRocket);

			jbBuy = new JButton("Buy");
			jbBuy.setBackground(Color.GREEN);
			jbBuy.setSize(width, depth);
			this.add(jbBuy);
			jbBuy.setVisible(true);

			jbCancel = new JButton("Cancel");
			jbCancel.setBackground(Color.GREEN);
			jbCancel.setSize(width, depth);
			this.add(jbCancel);
			jbCancel.setVisible(true);

			jbMovement = new JButton("Move");
			jbMovement.setBackground(Color.GREEN);
			jbMovement.setSize(width, depth);
			this.add(jbMovement);
			jbMovement.addActionListener(new JButtonActionListener());
			jbMovement.setVisible(true);


			

		}


		public Dimension getPreferredSize(){
			return new Dimension(gridWidth*10, gridHeight*10);
		}


		private class JButtonActionListener implements ActionListener{
			
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				if(arg0.getSource()== jbMovement){
					System.out.println("actionPerformed");
					repaint();

				}

			}



		}


		private class JRBSniperActionListener implements ActionListener {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub

			}

		}

		private class JRBRocketActionListener implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

			}

		}

	}
}
